package com.studentms.dao.impl;

import com.studentms.bean.Users;
import com.studentms.dao.DBUtils;
import com.studentms.dao.UsersDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

//继承DUtils 实现UserDao
public class UsersDaoImpl extends DBUtils implements UsersDao {


    @Override
    public Users login(String username, String password) {
        Users users = null;
        try {
            //查询语句
            String sql = "select * from users where loginname=? and password=?";
            ArrayList arrayList= new ArrayList();
            //把用户名和密码放到一个集合中
            arrayList.add(username);
            arrayList.add(password);
            //父类中的query();方法   会返回一个结果集
             resultSet = query(sql, arrayList);
             if (resultSet == null){
                 return null;
             }
            //对返回的结果集进行包装
            while(resultSet.next()){//判断是否有下一个
                users = new Users();
                users.setLoginName(username);
                //其中这里的密码一般不展示到页面,所以就不用setPassword
                users.setRealName(resultSet.getString("realname"));
                users.setUserId(resultSet.getInt("userid"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return users;
    }
}
